<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addmenus"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="permissions(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(scope.row.id)">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="dels(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-dialog
            title="编辑弹层"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="showDialog"
            @close="$refs.roleForm.resetFields()"
          >
            <el-form
              ref="roleForm"
              :model="roleForm"
              label-width="100px"
            >
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" @click="cancel()">取消</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="hSubmit"
                >确定</el-button>
              </el-col>
            </el-row>
          </el-dialog>

          <el-dialog :visible.sync="showlog">
            <Sydalog ref="Sydalog" :cui-id="cuiId" />
          </el-dialog>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getlists, dellists, addlists, getinfo, editlists } from '@/api/settings'
import Sydalog from './Sydalog.vue'
export default {
  components: { Sydalog },
  data() {
    return {
      isEdit: true,
      list: [],
      page: 1,
      pagesize: 2,
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      showlog: false,
      cuiId: ''
    }
  },
  created() {
    this.getmenus()
  },
  methods: {
    permissions(id) {
      console.log(id)
      this.cuiId = id
      this.showlog = true
      this.$nextTick(() => {
        this.$refs.Sydalog.getgougou()
      })
    },
    cancel() {
      this.showDialog = false
    },
    async edit(id) {
      this.showDialog = true

      const data = await getinfo(id)
      // console.log(data)
      this.roleForm = { ...data.data }
    },
    async update() {
      const data = await editlists(this.roleForm)
      console.log(data)
    },
    hSubmit() { this.isEdit ? this.update() : this.addmenuss() },
    async addmenus() {
      this.showDialog = true
      this.isEdit = false
    },
    async  addmenuss() {
      const { data: res } = await addlists(this.roleForm)
      console.log(res)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.page = 1
      this.getmenus()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getmenus()
    },
    async getmenus() {
      const { data: res } = await getlists({ page: this.page, pagesize: this.pagesize })
      console.log(res)
      this.list = res.rows
      this.total = res.total
    },
    dels(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const data = await dellists(id)
        console.log(data)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getmenus()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style></style>

